home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7943 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  45 lines

  1. Path: news.ichange.com!newsmaster
  2. From: Jesse Liberty <jl@staff.ichange.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: No struct in C++!!?
  5. Date: Thu, 15 Feb 1996 07:53:32 -0500
  6. Organization: AT&T
  7. Message-ID: <31232CCC.4679@staff.ichange.com>
  8. References: <1996Feb14.151620.5532@queens-belfast.ac.uk>
  9. NNTP-Posting-Host: 140.244.99.60
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (Win95; I)
  14. CC: jl@staff.ichange.com
  15.  
  16. Georg Woste wrote:
  17. > I found in different C++ books examples of C++ programms
  18. > which contain type declarations and definitions in the main() programm - for
  19. > example a struct  - as in C.
  20. > My question is, whether this is a contradiction
  21. > to the paradigm of C++.
  22.  
  23. In C++ a struct is EXACTLY like a class in every aspect (can have members and functions) except that the default inheritance 
  24. is public (not private) and the default access for members is public (not private).  Thus the distinction you are making 
  25. between a data only object and an object with methods is specious -- both structs and classes can be any of data-only, 
  26. method-only or both.
  27.  
  28. By convention, some C++ programmers use "struct" for data-only objects, but this is somewhat misleading; there is no reason 
  29. you can't write
  30.  
  31. struct Animal
  32. {
  33.     int myAge;
  34.     void SetAge(int age) { myAge = age); }
  35. };
  36.  
  37. In fact, if you explicitly use access specifiers (as I believe you should) you won't see ANY difference at all.
  38.  
  39. By the way, I cover this in detail on page 151 of Teach Yourself C++ In 21 Days. :-)
  40.  
  41. -- 
  42. Jesse Liberty    jl@staff.ichange.com
  43. AT&T             CIS: 72241,72
  44.